Back to course home
0% completed
Vote For New Content
Google Hard
Shortest Subarray with Sum at Least K (hard)
Solution: Shortest Subarray with Sum at Least K
Reverse Pairs (hard)
Solution: Reverse Pairs
Nth Magical Number (hard)
Solution: Nth Magical Number
Candy (hard)
Solution: Candy
Arithmetic Slices II - Subsequence (hard)
Solution: Arithmetic Slices II - Subsequence
Burst Balloons (hard)
Solution: Burst Balloons
Sum of Distances in Tree (hard)
Solution: Sum of Distances in Tree
Maximum Score of a Good Subarray (hard)
Solution: Maximum Score of a Good Subarray
Trapping Rain Water (hard)
Solution: Trapping Rain Water
First Missing Positive (hard)
Solution: First Missing Positive
Arithmetic Slices (medium)
Problem Statement
Given an integer array nums, return the number of arithmetic subarrays of nums.
If any integer array consists of at least three elements and if the difference between any two consecutive elements is the same, it is called arithmetic.
A subarray is a contiguous subsequence of the array.
Examples
Example 1:
- Input:
[1, 3, 5, 7, 9, 10, 11] - Expected Output:
7 - Justification: The segments that form arithmetic sequences are
[1, 3, 5],[3, 5, 7],[5, 7, 9],[1, 3, 5, 7],[3, 5, 7, 9],[1, 3, 5, 7, 9], and[9, 10, 11]. Each of these segments has a constant difference of 2 between consecutive elements.
Example 2:
- Input:
[7, 7, 7, 7] - Expected Output:
3 - Justification: The qualifying segments are
[7, 7, 7],[7, 7, 7, 7](considering the first three and then all four elements), and the last three elements[7, 7, 7]. Each segment has a constant difference of 0 between its elements.
Example 3:
- Input:
[1, 2, 4, 6, 8] - Expected Output:
3 - Justification: Only three segments
[2,4,6],[4, 6, 8]and[2, 4, 6, 8]form arithmetic sequences with a constant difference of 2.
Try it yourself
Try solving this question here:
Java
Saved to Cloud
. . . .
Solution: 2 Keys Keyboard
Solution: Arithmetic Slices
Mark as Completed
On this page
Problem Statement
Examples
Try it yourself